package com.data.util

import org.apache.commons.io.FileUtils
import org.springframework.web.multipart.commons.CommonsMultipartFile
import java.io.File
import java.io.IOException
import javax.servlet.http.HttpServletResponse

/**
 * 图片响应
 *
 * @author:HingLo
 * @create 2017-09-28 17:13
 */
object ImageLookUtils {
    /**
     * 图片响应
     *
     * @param inputStream
     * @param response
     */
    fun responseImage(inputStream: ByteArray, response: HttpServletResponse) {
        try {
            response.contentType = "image/png"
            val stream = response.outputStream
            stream.write(inputStream)
            stream.flush()
            stream.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }

    }

    /**
     * 保存
     * * @param path
     *
     * @param inputStream
     * @return
     */
    fun saveFile(path: String, inputStream: CommonsMultipartFile): Boolean {
        return try {
            FileUtils.copyInputStreamToFile(inputStream.inputStream, File(path))
            true
        } catch (e: IOException) {
            false
        }


    }

    /**
     * 读取图片
     *
     * @param path 传入文件的路径
     * @return 返回文件二进制文件
     */
    fun readImage(path: String): ByteArray? {
        val file = File(path)
        try {
            return FileUtils.readFileToByteArray(file)
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return null
    }

    /**
     * 读取图片，传入读取的文件对象
     *
     * @return 返回文件二进制文件
     */
    private fun readImage(file: File): ByteArray? {
        try {
            return FileUtils.readFileToByteArray(file)
        } catch (e: IOException) {
            e.printStackTrace()
        }

        return null
    }

    /**
     * 读取图片，传入读取的文件对象
     *
     * @return 返回文件二进制文件
     */
    fun readImage(PATH: String, fileName: String): ByteArray? {
        val path: String = if (CheckDataUtils.stringUtils(fileName)) {
            PATH + fileName
        } else {
            //默认图片
            PATH + FinalStringUtils.IMAGEDEFAULT
        }
        val file = File(path)
        return if (file.isFile) {
            ImageLookUtils.readImage(file)
        } else null

    }

    /**
     * 根据文件对象删除文件
     *
     * @param file
     * @return
     */
    fun deleteImage(file: File): Boolean = file.delete()

    /**
     * 根据文件对象删除文件
     *
     * @param path
     * @return
     */
    fun deleteImage(path: String): Boolean {
        val file = File(path)
        return if (file.isFile) {
            file.delete()
        } else false
    }

    /***
     * 删除目录
     */
    fun deleteDir(path: String): Boolean {
        val dir = File(path)
        if (dir.exists() && dir.isDirectory) {
            val tmp = dir.listFiles()
            for (i in tmp!!.indices) {
                if (tmp[i].isDirectory) {
                    deleteDir(path + "/" + tmp[i].name)
                } else {
                    tmp[i].delete()
                }
            }
            return dir.delete()
        }
        return false
    }

    /**
     * 获取文件的后缀
     *
     * @param file
     * @return
     */
    fun getFileSuffix(file: CommonsMultipartFile?): String? {
        if (file != null) {
            val filename = file.originalFilename
            return filename.substring(filename.lastIndexOf("."))
        }
        return null


    }


}
